package com.ddwl.shop.dao;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.GlobalException;
import com.ddwl.schema.bo.shop.QueryShopBannerBo;
import com.ddwl.shop.dao.mapper.ShopBannerMapper;
import com.ddwl.shop.dao.model.ShopBanner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;

/**
 * (ShopBanner)表数据库访问层
 * @author wenbin
 */
@Slf4j
@Repository
public class ShopBannerDao extends TkDao<ShopBannerMapper, ShopBanner> {

    public List<ShopBanner> queryShopBannerList(QueryShopBannerBo bo) {
        Example example = new Example(ShopBanner.class);
        example.and().andEqualTo("id",bo.getId())
                .andIn("id",bo.getIdList())
                .andEqualTo("title",bo.getTitle())
                .andEqualTo("position",bo.getPosition())
                .andEqualTo("effect",bo.getEffect())
                .andEqualTo("shopId",bo.getShopId())
                .andGreaterThan("gmtCreated",bo.getStartTime())
                .andLessThan("gmtCreated",bo.getEndTime())
                .andEqualTo("dflag",0);
        return baseMapper.selectByExample(example);
    }

    public void updateShopBanner(ShopBanner shopBanner, String shopId) {
        Example example = new Example(ShopBanner.class);
        example.and().andEqualTo("id",shopBanner.getId())
                .andEqualTo("shopId",shopId);
        Integer i =baseMapper.updateByExampleSelective(shopBanner,example);
        if(i<1){
            log.error("修改店铺banner信息失败 shopId={}", JSON.toJSONString(shopBanner.getId()));
            throw new GlobalException("修改店铺banner信息失败");
        }
    }

    public void delete(String id, String userId,String shopId) {
        ShopBanner shopBanner=new ShopBanner();
        shopBanner.setId(id);
        shopBanner.setGmtModified(Instant.now());
        shopBanner.setModifiedUserId(userId);
        shopBanner.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        Example example = new Example(ShopBanner.class);
        example.and().andEqualTo("id",id)
                .andEqualTo("shopId",shopId);
        baseMapper.updateByExampleSelective(shopBanner,example);
    }
    public void deleteByShopId(String userId,String shopId) {
        ShopBanner shopBanner=new ShopBanner();
        shopBanner.setGmtModified(Instant.now());
        shopBanner.setModifiedUserId(userId);
        shopBanner.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        Example example = new Example(ShopBanner.class);
        example.and()
                .andEqualTo("shopId",shopId);
        baseMapper.updateByExampleSelective(shopBanner,example);
    }
}